﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Repository;
using JinfuyuanProject.Repository.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JinfuyuanProject.Service
{
    public class HomeService : IHomeService
    {
        private readonly IHomeRepository _homeRepository;

        public HomeService(IHomeRepository homeRepository)
        {
            _homeRepository = homeRepository;
        }
        #region 基础方法
        public bool Delete(Home home)
        {
            return _homeRepository.Delete(home);
        }

        public IQueryable<Home> GetList()
        {
            return _homeRepository.GetList();
        }

        public Home GetModel(int Id)
        {
            return _homeRepository.GetModel(Id);
        }

        public int Insert(Home home)
        {
            return _homeRepository.Insert(home);
        }

        public bool Update(Home home)
        {
            return _homeRepository.Update(home);
        }
        public IEnumerable<T> QueryBySql<T>(string sql)
        {
            return _homeRepository.QueryBySql<T>(sql);
        }
        #endregion

        #region 业务方法
        public List<HomeVM> GetRoomDetail()
        {
            var homeRooms=QueryBySql<HomeVM>(@"
select h.*, p.PackageName, c.LiveTime, pt.ParturientName
from Home h 
left join Package p on h.PackageId = p.Id
left join Contract c on h.Id = c.HomeId
left join Parturient pt on h.Id = pt.HomeId").ToList();
            foreach (var room in homeRooms)
            {
                if (room.State == 1 && room.LiveTime != null)
                {
                    room.DaysStayed = (DateTime.Now - room.LiveTime.Value).Days; // 计算已入住天数
                    if (room.DaysStayed == 0)  //再今天入住显示入住1天而不是显示入住0天
                    {
                        room.DaysStayed++;
                    }
                }
            }
            return homeRooms;
        }
        public ParturientVM GetParturientVM(int id)
        {
            return QueryBySql<ParturientVM>($@"select p.*,c.LiveTime,c.Id as ContractId,b.BirthType 
                             from Parturient p
                             left join Contract c on p.Id = c.ParturientId
                             left join  Home h on c.HomeId = h.Id
                             left join  Baby b ON p.Id = b.ParturientId
                             where p.HomeId={id}").ToList().FirstOrDefault();
        }
        public List<Baby> GetBabyDetail(int id)
        {
            return _homeRepository.GetBabyDetail(id);
        }
        public List<ServiceDoVM> GetServiceDoVM(int ParturientId)
        {
            return _homeRepository.GetServiceDoVM(ParturientId);
        }
        public Hashtable ProductAPI(ProductFilter filter)
        {
            int count;
            var list = _homeRepository.ProductAPI();
            Hashtable hs = new Hashtable();
            hs["code"] = 0;
            hs["msg"] = "success";
            hs["data"] = MakeStaffList(list, filter, out count);
            hs["count"] = count;
            return hs;
        }
        #endregion

        #region 公共方法
        public IQueryable<Product> MakeStaffList(IQueryable<Product> product, ProductFilter filter, out int count)
        {
            if (!string.IsNullOrEmpty(filter.ProductName))
            {
                product = product.Where(p => p.ProductName.Contains(filter.ProductName));
            }
            if (filter.IsDelete != null)
            {
                product = product.Where(p => p.IsDelete == filter.IsDelete);
            }
            count = product.Count();
            if (filter.page != 0 && filter.limit != 0)
            {
                return product = product.OrderBy(x => x.Id).Skip((filter.page - 1) * filter.limit).Take(filter.limit);
            }
            else
            {
                return product;
            }
        }
        #endregion
    }
}
